######################################
# Makefile对缩进非常敏感。你需要使用Tab键进行缩进，而不是空格。否则，Make可能会报错,文件路径要用 / 来间隔。
BUILD_DIR = Build_make
TARGET = AIR32Lab
DEBUG = 0
OPT = -Ofast
# OPT = -O0

# 宏定义
AS_DEFS = 
C_DEFS =  
ifeq ($(DEBUG), 1)
C_DEFS += -DDEBUG
else
C_DEFS += -DRELEASE
endif

# 头文件
AS_INCLUDES = 
C_INCLUDES =  \
-ISrc \
-ILibs/CMSIS/Include \
-ILibs/AIR32F10xLib/inc \
-ILibs/AIR32F10xLib/lib/cryptlib \
-ILibs/AIR32_USB-FS-Device_Driver/inc \

# 源文件
C_SOURCES =  \
Libs/AIR32F10xLib/src/misc.c \
Libs/AIR32F10xLib/src/air32f10x_rcc.c \
Libs/AIR32F10xLib/src/air32f10x_rcc_ex.c \
Libs/AIR32F10xLib/src/air32f10x_gpio.c \
Libs/AIR32F10xLib/src/air32f10x_usart.c \
Libs/AIR32F10xLib/src/air32f10x_spi.c \
Libs/AIR32F10xLib/src/air32f10x_tim.c \
Libs/AIR32F10xLib/src/air32f10x_dma.c \
Libs/AIR32F10xLib/src/air32f10x_dac.c \
Libs/AIR32F10xLib/src/system_air32f10x.c \
Src/air32f10x_it.c \
Src/sys.c \
Src/usart1.c \
Src/spi1.c \
Src/w25qxx.c \
Src/printf.c \
Src/led.c \
Src/font.c \
Src/dac1.c \
Src/tim2.c \
Src/tim3.c \
Src/tim4.c \
Src/tim5.c \
Src/curtain.c \
Src/volleyball.c \
Src/5m3x.c \
Src/old5m3x.c \
Src/main.c \
Src/help_curtain.c
ASM_SOURCES =  \
Libs/STARTUP/gcc/startup_air32f10x_big.s
# Libs/STARTUP/gcc/startup_air32f10x.s

# 链接器
PREFIX = arm-none-eabi-
CC = $(PREFIX)gcc
AS = $(PREFIX)gcc -x assembler-with-cpp
CP = $(PREFIX)objcopy
SZ = $(PREFIX)size
OD = $(PREFIX)objdump
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S

# 编译参数
CPU = -mcpu=cortex-m3
MCU = $(CPU) -mthumb $(FPU) $(FLOAT-ABI)
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections

ifeq ($(DEBUG), 1)
CFLAGS += -g -ggdb
endif

CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"

LDSCRIPT = Libs/STARTUP/gcc/linker/AIR32F103XB_FLASH.ld

LIBS = -lc -lm -lnosys 
LIBDIR = 
LDFLAGS = $(MCU) -specs=nano.specs -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections

#######################################
# all
#######################################
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.s=.o)))
vpath %.s $(sort $(dir $(ASM_SOURCES)))

# 用于生成用户代码的asm清单
MY_CFILES = $(notdir $(wildcard Src/*.c))
MY_OBJS = $(addprefix $(BUILD_DIR)/, $(MY_CFILES:.c=.o))
vpath %.asm $(sort $(dir $(MY_OBJS)))
MY_ASMS = $(addprefix $(BUILD_DIR)/, $(MY_CFILES:.c=.asm))
vpath %.asm $(sort $(dir $(MY_ASMS)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-ahlms=$(BUILD_DIR)/$*.lst $< -o $@

$(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@

$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	

# 将 Src 下的所有代码生成对应的asm文件
$(BUILD_DIR)/%.asm: $(BUILD_DIR)/%.o
	$(OD) -r --source-comment=@ -d -w -S $< > $@

# 这是一个假目标,用以产生上面的调用
$(BUILD_DIR)/$(TARGET).fake : $(MY_ASMS) Makefile
	@echo "all the user src code asm file are generated!"

# 建立BUILD_DIR文件夹
$(BUILD_DIR):
	mkdir $@

#######################################
# clean up
#######################################
.PHONY: clean
clean:
ifeq ($(OS),Windows_NT)  
	-powershell -Command "Remove-Item -Path $(BUILD_DIR) -Recurse"
endif

#######################################
# asm
#######################################
asm: $(BUILD_DIR)/$(TARGET).fake

#######################################
# download
#######################################
.PHONY: flash
flash:all
	pyocd flash -t AIR32F103CC --pack Libs/Keil.AIR32F103_DFP.1.1.9.pack -f 1M $(BUILD_DIR)/$(TARGET).hex


#######################################
# reboot
#######################################
.PHONY: reboot
reboot:
	pyocd reset -t AIR32F103CC --pack Libs/Keil.AIR32F103_DFP.1.1.9.pack

#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)
# *** EOF ***